package Project_MemberLogic

import (
	"CRM_rorm/logic/StaffLogic"
	"CRM_rorm/model"
	"CRM_rorm/response"
	"CRM_rorm/service"
	"github.com/gin-gonic/gin"
)

func ProjectMemberAdd(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var member model.Project_Member
	err := ctx.Bind(&member)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ProjectMemberModel.Insert(member)
	if err != nil {
		response.Fail(ctx, nil, "增加失败")
		return
	}
	response.Success(ctx, nil, "增加成功")
}

func ProjectMemberDeleteSoft(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var member model.Project_Member
	err := ctx.Bind(&member)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ProjectMemberModel.DeleteSoft(member)
	if err != nil {
		response.Fail(ctx, nil, "软删除失败")
		return
	}
	response.Success(ctx, nil, "软删除成功")
}

func ProjectMemberDelete(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var member model.Project_Member
	err := ctx.Bind(&member)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ProjectMemberModel.Delete(member)
	if err != nil {
		response.Fail(ctx, nil, "删除失败")
		return
	}
	response.Success(ctx, nil, "删除成功")
}

func ProjectMemberFind(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var member model.Project_Member
	err := ctx.Bind(&member)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	member, err = service.Svc.ProjectMemberModel.FindOne(member)
	if err != nil {
		response.Fail(ctx, nil, "查找失败")
		return
	}
	response.Success(ctx, gin.H{
		"project_id":          member.Project_id,
		"project_member_id":   member.Project_Member_id,
		"project_member_name": member.Project_Member_name,
		"project_member_tel":  member.Project_Member_Tel,
		"role":                member.Role,
	}, "查询成功")

}

func ProjectMemberUpdate(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var company model.Company
	err := ctx.Bind(&company)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.CompanyProjectModel.Update(company)
	if err != nil {
		response.Fail(ctx, nil, "更新失败")
		return
	}
	response.Success(ctx, nil, "更新成功")
}
